var netEngine = require('netEngine');

cc.Class({
    extends: cc.Component,

    properties: {
        websocket_url : cc.String,
        roomIdLabel : cc.EditBox,
        noticeLabel : cc.Label,
        showNoticeClear:0
    },

    onLoad: function() {
        if (Global.game != null) {
            clearInterval(Global.game.showNoticeClear);
        }
        clearInterval(this.showNoticeClear);

        Global.login = this;

        this.noticeLabel.node.active = false;

        //如果是扫二维码进入的 获取get里的房间号
        var roomid = Global.getQueryVariable("roomid");
        if (roomid > 999) {
            this.roomIdLabel.string = roomid;
            this.joinRoom();
        }
        this.wx_share();
    },
    wx_share: function(){
        //判断是不是微信浏览器
        if (!Global.isWeiXin()) return;

        //修改微信的分享给朋友时的界面
        var xhr = new XMLHttpRequest();
        var url = "/index/wechat/ajaxSignPackage";
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                var wxoptions = {
                    sign_pkg: JSON.parse(xhr.responseText),
                    wx_share: {
                        "title": "K米蹦笨跳",
                        "desc": "看谁吃的星星更多,快来试试看",
                        "link": location.origin + location.pathname,
                        "imgurl": "/static/common/images/star.png"
                    }
                }
                console.log(wxoptions.wx_share.link);
                initWeiXin(wxoptions);
            }
        };
        xhr.open("GET", url+"?url="+encodeURIComponent(window.location.href), true);
        xhr.send();
    },
    createRoom : function() {
        Global.isJoin = false;
        var self = this;
        Global.ws = new WebSocket(this.websocket_url);

        //发送创建房间的信息
        Global.ws.onopen = function () {
            var sendData = {
                type: "createRoom",
                screenWidth: self.node.width
            };
            var sendString = JSON.stringify(sendData);
            Global.ws.send(sendString);
        };
        //服务端返回成功后,进入游戏场景
        Global.ws.onmessage = netEngine.onmessage;

        Global.ws.onclose = function() {
            cc.log('websocket close');
        };
        Global.ws.onerror = function() {
            alert ('Error in connection');
            self.noticeLabel.node.active = false;
        };

        this.noticeLabel.node.active = true;
        this.showNotice("正在连接服务器");
    },
    joinRoom: function() {
        Global.isJoin = true;
        var self = this;
        Global.ws = new WebSocket(this.websocket_url);

        Global.ws.onopen = function () {
            var roomId = self.roomIdLabel.string;
            var sendData = {
                type: "joinRoom",
                screenWidth: self.node.width,
                roomId: roomId
            };
            var sendString = JSON.stringify(sendData);
            Global.ws.send(sendString);
        };
        //服务端返回成功后,进入游戏场景
        Global.ws.onmessage = netEngine.onmessage;

        Global.ws.onclose = function() {
            cc.log('websocket close');
        }
        Global.ws.onerror = function() {
            alert ('Error in connection');
            self.noticeLabel.node.active = false;
            Global.ws.close()
        }

        this.noticeLabel.node.active = true;
        this.showNotice("正在连接服务器");
    },

    //随机匹配对手加入
    randMode: function() {
        Global.isJoin = true;
        var self = this;
        Global.ws = new WebSocket(this.websocket_url);
        Global.ws.onopen = function () {
            var sendData = {
                type: "randMode",
                screenWidth: self.node.width,
            };
            var sendString = JSON.stringify(sendData);
            Global.ws.send(sendString);
        };
        //服务端返回成功后,进入游戏场景
        Global.ws.onmessage = netEngine.onmessage;

        Global.ws.onclose = function() {
            cc.log('websocket close');
        }
        Global.ws.onerror = function() {
            alert ('Error in connection');
            self.noticeLabel.node.active = false;
        }

        this.noticeLabel.node.active = true;
        this.showNotice("正在连接服务器");
    },
    showNotice : function(content) {
        var n = 0;
        var self = this;
        this.showNoticeClear = setInterval(function(){
            if (self.noticeLabel == null) return;
            switch (n%3) {
                case 0:
                    self.noticeLabel.string = content + ".";
                    break;
                case 1:
                    self.noticeLabel.string = content + "..";
                    break;
                case 2:
                    self.noticeLabel.string = content + "...";
                    break;
            }
            n ++;
        },700);
    },
    readyState : function() {
        //cc.log(Global.ws.readyState)
        Global.ws.close();
    }

});
